﻿// Author: AustralTek 
// Creation date: 2012/05/09
// http://www.australtek.com
// Copyright: 2009 - 2012

/* 
Glossary:
*/

(function () {
    try {
        //#region Transition

        var _Transition = { Public: {} };
        _Transition.Base = function (MyReference, ChildBase) {
            try {
                //#region Attributes

                if(!ChildBase)
                    ChildBase = {};

                if(!MyReference.Public)
                    MyReference.Public = {};

                //#region Properties

                MyReference.CurrentValue = null;
                MyReference.Direction = 1;
                MyReference.Events = null;
                MyReference.Finished = false;
                MyReference.FunctionMove = null;
                MyReference.LimitEnd = null;
                MyReference.LimitStart = null;

                MyReference.InitializeOptions = {
                    CurrentValue: 0
                    , Events: { Error: [] }
                    , FunctionMove: _Transition.FunctionsMoveColl.LinearIncr
                    , LimitEnd: Infinity
                    , LimitStart: 0
                };

                //#endregion

                //#region Methods

                ChildBase.FireEventError =
                MyReference.FireEventError = function(error) {
                    try
                    {
                        for(var handlerIndex = 0; handlerIndex < MyReference.Events.Error.length; handlerIndex++)
                            MyReference.Events.Error[handlerIndex](error, MyReference.Public);
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                };
                ChildBase.GetCurrentValue =
                MyReference.Public.GetCurrentValue =
                MyReference.GetCurrentValue = function() {
                    try {
                        return MyReference.CurrentValue;
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                    return null;
                };
                ChildBase.GetLimitEnd =
                MyReference.Public.GetLimitEnd =
                MyReference.GetLimitEnd = function() {
                    try {
                        return MyReference.LimitEnd;
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                    return null;
                };
                ChildBase.GetLimitStart =
                MyReference.Public.GetLimitStart =
                MyReference.GetLimitStart = function() {
                    try {
                        return MyReference.LimitStart;
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                    return null;
                };
                ChildBase.GetStatus =
                MyReference.Public.GetStatus =
                MyReference.GetStatus = function() {
                    var status = new _Transition.FunctionMoveResult();
                    try {
                        status.Value = MyReference.CurrentValue;
                        status.Finished = MyReference.Finished;
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                    return status;
                };
                ChildBase.Initialize =
                MyReference.Initialize = function(options) {
                    try {
                        /* 
                            For now remove the InitializeOptions struct From MyReference, fill it, isnert it in 
                        MyReference and then add it again.
                        */
                        var initializeOptions = MyReference.InitializeOptions;

                        delete MyReference.InitializeOptions;

                        ParseAttributes.Parse(options, initializeOptions);

                        ParseAttributes.Parse(initializeOptions, MyReference);

                        MyReference.InitializeOptions = initializeOptions;

                        if ((!options) || (!options.Events) || (!options.Events.onError))//Modified this lines when all the events of the ID Widgets will be Arrays.
                            MyReference.Events.Error = [function (error) { window.status = Error.message; } ];
                        if (!MyReference.Events.Error)
                            MyReference.Events.Error = [function (error) { window.status = Error.message; } ];

                        MyReference.UpdateDirection();
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                };
                ChildBase.Move =
                MyReference.Public.Move =
                MyReference.Move = function(iterationsCount, doMove) {
                    var newStatus = new _Transition.FunctionMoveResult();
                    try {
                        if(!iterationsCount)
                            iterationsCount = 1;

                        newStatus.Value = MyReference.CurrentValue;

                        for(var iterationIndex = 1; iterationIndex <= iterationsCount; iterationIndex++)
                            newStatus.Value = MyReference.FunctionMove(newStatus.Value);

                        if((MyReference.Direction > 0) && (newStatus.Value >= MyReference.LimitEnd)
                            || (MyReference.Direction < 0) && (newStatus.Value <= MyReference.LimitEnd))
                        {
                            newStatus.Value = MyReference.LimitEnd;
                            newStatus.Finished = true;
                        }

                        if(doMove !== false)
                        {
                            MyReference.CurrentValue = newStatus.Value;
                            MyReference.Finished = newStatus.Finished;
                        }
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                    return newStatus;
                };
                ChildBase.Reset =
                MyReference.Public.Reset =
                MyReference.Reset = function() {
                    try {
                        MyReference.CurrentValue = MyReference.LimitStart;
                        MyReference.Finished = false;
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                };
                ChildBase.SetCurrentValue =
                MyReference.Public.SetCurrentValue =
                MyReference.SetCurrentValue = function(newCurrentValue) {
                    try {
                        MyReference.CurrentValue = newCurrentValue;
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                };
                ChildBase.SetLimitEnd =
                MyReference.Public.SetLimitEnd =
                MyReference.SetLimitEnd = function(newLimitEnd) {
                    try {
                        MyReference.LimitEnd = newLimitEnd;

                        MyReference.UpdateDirection();
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                };
                ChildBase.SetLimitStart =
                MyReference.Public.SetLimitStart =
                MyReference.SetLimitStart = function(newLimitStart) {
                    try {
                        MyReference.LimitStart = newLimitStart;

                        MyReference.UpdateDirection();
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                };
                ChildBase.UpdateDirettion =
                MyReference.UpdateDirection = function() {
                    try {
                        MyReference.Direction = MyReference.LimitEnd - MyReference.LimitStart;
                    }
                    catch(Error) { MyReference.FireEventError(Error, window.location.href, arguments.callee, ""); }
                };

                //#endregion

                //#region Events

                

                //#endregion
            }
            catch(Error) { ShowError(Error.message, window.location.href, arguments.callee, ""); }
        };
        _Transition.Constructor = function (options) {
            try {
                var instance = {};
                
                _Transition.Base(instance);

                instance.Initialize(options);

                //#region Public

                //#region Methods

                ObjectExtend(instance.Public, this);

                //#endregion

                //#endregion
            }
            catch(Error) { ShowError(Error.message, window.location.href, arguments.callee, ""); }
        };

        _Transition.FunctionMoveResult = function() {
            this.Value = null;
            this.Finished = false;
        };

        _Transition.Public.FunctionsMoveColl = 
        _Transition.FunctionsMoveColl = {
            LinearDecr: function(x) { return x - 1; }
            , LinearIncr: function(x) { return x + 1; }
        };

        _Transition.Public.Inherit = function(obj) {
            try {
                var childBase = GetLastBaseProperty(obj);

                _Transition.Base(obj, childBase);

                return childBase;
            }
            catch(Error) { ShowError(Error.message, window.location.href, arguments.callee, ""); }
        };

        //#endregion

        window.Transition = _Transition.Constructor;

        ObjectExtend(_Transition.Public, window.Transition);
    }
    catch(Error) { ShowError(Error.message, window.location.href, arguments.callee, ""); }
})();